﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Csharp_manytomany_415.Data;
using Csharp_manytomany_415.Data.Models;
using Csharp_manytomany_415.Models;

namespace Csharp_manytomany_415.Adapters
{
    public class MovieDataAdapter : IMovieAdapter
    {
        public List<Data.Models.Actor> GetActors()
        {
            using (MoviesDbContext peanutbutter = new MoviesDbContext())
            {
                return peanutbutter.Actors.ToList();
            }
        }





        public void AddActor(Models.ActorsCreateVm newActor)
        {
            using (MoviesDbContext peanutbutter = new MoviesDbContext()) 
            {
                Actor tempActor = new Actor();
                tempActor.City = newActor.City;
                tempActor.FirstName = newActor.FirstName;
                tempActor.LastName = newActor.LastName;
                peanutbutter.Actors.Add(tempActor);
                peanutbutter.SaveChanges();
              
            }
        }


        // Get movies
        public List<Movie> GetMovies()
        {
            using (MoviesDbContext db = new MoviesDbContext())
            {
                return db.Movies.ToList();
            }

        }



//

        // get all Actors that are in movie X
        public MovieDetailsVm GetMovieActors(int mid)
        {
            MovieDetailsVm myList = new MovieDetailsVm();
            using (MoviesDbContext db = new MoviesDbContext())
            {
               //var q = db.Movies.Include("Actors").Where(a => a.MovieId == mid).ToList();
               //return q;
                var y = db.Movies.FirstOrDefault(m => m.MovieId == mid);
                var x = db.MovieActor.Where(a => a.MovieId == y.MovieId).Select(f => new ActorVm()
                        {
                            FirstName = f.Actor.FirstName,
                            LastName = f.Actor.LastName,
                            City = f.Actor.City
                        }).ToList();

                myList.MovieTitle = y.Title;
                myList.AllActors = x;
                return myList;
            }
        }

        public Movie GetMovie(int mid)
        {
            throw new NotImplementedException();
        }
    }
}